from django.conf import settings


class SqlData:
    # SqlDetail表的解析，对单条sql进行测试时用
    def __init__(self, queryset):
        self.queryset = queryset
        self.db_info = None
        self.db_type = None
        self.sql_template = None
        self.sql_push_fields = None
        self.sql_pull_fields = None
        self.run()

    def get_database_info(self):
        queryset = self.queryset.database
        db_types = settings.MODELS_CHOICES['database']['db_type']
        self.db_type = [db[1] for db in db_types if db[0] == queryset.db_type][0]
        self.db_info = {
            "host": queryset.db_ip,
            "port": queryset.db_port,
            "user": queryset.db_user,
            "password": queryset.db_password,
            "db": queryset.db_name,
            "charset": 'utf8'
        }

    # 得到sql语句以及入参出参
    def get_sql_info(self):
        self.sql_template = self.queryset.sql_template  # sql语句
        self.sql_push_fields = self.queryset.sql_push_fields  # 入参
        self.sql_pull_fields = self.queryset.sql_pull_fields  # 出参

    def run(self):
        self.get_database_info()
        self.get_sql_info()
